package com.baidu.customview.widget.viewgroup;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.TextView;

import com.baidu.customview.R;

/**
 * Created by JGW-android on 2016/3/24.
 */
public class SVRecordView extends ViewGroup {
    private final int DEFAULT_CHILD = 4;
    private int lineColor;
    private int iconId;
    private int leftWidth = 50;//use for drawing icon
    private int middleWidth = 150;//use for drawing icon
    private Paint linePaint;
    private Paint bitmapPaint;
    private Bitmap bitmap;
    private int bw;
    private int bh;

    private String txt1;
    private String txt2;
    private String txt3;
    private String txt4;

    private int txtSize = 10;

    public SVRecordView(Context context) {
        super(context);
        initProperty(context);
    }

    private void initProperty(Context context) {
        setWillNotDraw(false);//
        lineColor = Color.GRAY;
        iconId = R.mipmap.circle_blue;
        addChildViews(DEFAULT_CHILD, context);

        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        linePaint.setColor(lineColor);

        bitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.circle_blue);
        bw = bitmap.getWidth();
        bh = bitmap.getHeight();
    }

    private void addChildViews(int childNum, Context context) {
        for (int i = 0; i < childNum; i++) {
            addView(new TextView(context));
        }
    }

    public SVRecordView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initProperty(context);
    }

    public SVRecordView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);//measure children`s size
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawLine(leftWidth / 2, 0, leftWidth / 2, getHeight(), linePaint);
        canvas.drawLine(leftWidth + 2, getHeight() - 2, getWidth(), getHeight() - 2, linePaint);

        canvas.drawBitmap(bitmap, (leftWidth - bw) / 2, (getHeight() - bh) / 2, bitmapPaint);

        initLayout();
    }

    private void initLayout() {
        if (getChildCount() == DEFAULT_CHILD) {
            ViewGroup.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, getHeight() / 2);
            TextView tv1 = ((TextView) getChildAt(0));
            tv1.setLayoutParams(params);
            tv1.setTextColor(Color.BLACK);
            tv1.setTextSize(txtSize);
            tv1.setText(txt1);
            tv1.setGravity(Gravity.BOTTOM);
            tv1.layout(leftWidth, 0, leftWidth + middleWidth, getHeight() / 2);

            TextView tv2 = ((TextView) getChildAt(1));
            tv2.setLayoutParams(params);
            tv2.setGravity(Gravity.BOTTOM);
            tv2.setTextColor(Color.BLACK);
            tv2.setTextSize(txtSize);
            tv2.setText(txt2);
            tv2.layout(leftWidth + middleWidth, 0, getWidth(), getHeight() / 2);

            TextView tv3 = ((TextView) getChildAt(2));
            tv3.setLayoutParams(params);
            tv3.setTextColor(Color.GRAY);
            tv3.setTextSize(txtSize);
            tv3.setText(txt3);
            tv3.layout(leftWidth, getHeight() / 2, leftWidth + middleWidth, getHeight());

            TextView tv4 = ((TextView) getChildAt(3));
            tv4.setLayoutParams(params);
            tv4.setTextColor(Color.GRAY);
            tv4.setTextSize(txtSize);
            tv4.setText(txt4);
            tv4.layout(leftWidth + middleWidth, getHeight() / 2, getWidth(), getHeight());
        }
    }

    public void setText(String txt1, String txt2, String txt3, String txt4) {
        this.txt1 = txt1;
        this.txt2 = txt2;
        this.txt3 = txt3;
        this.txt4 = txt4;
        postInvalidate();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int i = l;
    }
}
